﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;

public partial class WebUI_Shop_GridList : LimitedPageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack && DataSource == null)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("ID");
            dt.Columns.Add("Name");
            dt.Columns.Add("Value");

            Random rand = new Random(DateTime.Now.Millisecond);
            for (int i = 0; i < DataRowsCount; i++)
            {

                dt.Rows.Add("S_" + i.ToString("00000"), GenRandName(rand), rand.NextDouble());
            }
            DataSource = dt;
        }
    }
    public string GenRandName(Random rand)
    {
        int length = rand.Next(1, 10);
        StringBuilder sb = new StringBuilder(length);
        for (int i = 0; i < length; i++)
        {
            if (i == 0)
            {
                sb.Append((Char)rand.Next(65, 90));
            }
            else
            {
                sb.Append((Char)rand.Next(97, 122));
            }
        }
        return sb.ToString();
    }
    public const int DataRowsCount = 100;
    public const int PageCapacity = 12;
    static DataTable DataSource = null;

    [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
    public DataTable GetDataSource(int pageNo)
    {
        DataTable result = DataSource.Clone();
        if (pageNo < 1)
        {
            return result;
        }

        int startRowIndex = GetStartRowIndex(pageNo, DataRowsCount, PageCapacity);

        if (startRowIndex < 0)
        {
            return result;
        }

        for (int i = startRowIndex; i < DataSource.Rows.Count && i < startRowIndex + PageCapacity; i++)
        {
            result.Rows.Add(DataSource.Rows[i].ItemArray);
        }
        return result;
    }

    private int GetStartRowIndex(int pageNo, int datarowCount, int pageCapacity)
    {
        int start = (pageNo - 1) * pageCapacity;
        if (datarowCount <= start)
        {
            start = -1;
        }
        return start;
    }
}